package com.example.week04_work2;

/**
 * 通过变量来判断子线程是否结束.
 * @author nhsoft.ll
 */
public class VariableDemo {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        VariableDemoSumTask sumTask = new VariableDemoSumTask();
        sumTask.start();

        Boolean b = true;
        while(b){
            if(sumTask.getFinish()){
                System.out.println("异步计算结果为：" + sumTask.getResult());
                b = false;
            }
        }
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

    }
}

class VariableDemoSumTask extends Thread {

    private int result;

    private volatile Boolean finish = false;

    public int getResult() {
        return result;
    }

    public Boolean getFinish() {
        return finish;
    }

    @Override
    public void run() {
        result = sum();
        System.out.println("线程运行结果:" + result);
        finish = true;
    }

    private int sum() {
        return fibo(36);
    }

    private int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }
}

